package cn.kais.drawer.unit;

import cn.kais.drawer.Commons;
import cn.kais.drawer.UnitTools;
import cn.kais.drawer.bean.BeanPicture;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 网络图片
 */
public class UnitPicture {

    /**
     * 转换图片流
     */
    public static BufferedImage iPictureBuffer(String src) {
        try {
            URL url = new URL(src);
            return ImageIO.read(url);
        } catch (IOException e) {
            System.out.println("未获图片流信息,这次是真的");
            return null;
        }
    }

    /**
     * 转换图片流
     */
    public static BufferedImage iPictureBuffer(String src, String dft) {
        try {
            URL url = new URL(src);
            System.out.println("有图有真相");
            return ImageIO.read(url);
        } catch (IOException e) {
            System.out.println("未获图片流信息,使用默认图片");
            return iPictureBuffer(dft);
        }
    }

    /**
     * 测试 最原始图片
     */
    public void test() {
        String src = "https://g.csdnimg.cn/common/csdn-footer/images/badge.png"; // 竖图
//        String src = "https://pic.rmb.bdstatic.com/bjh/5748977058196cd9fbdb1dd1e2aea5b68191.jpeg"; // 竖图
        BufferedImage ima = iPictureBuffer(src, src);
        if (null == ima) {
            System.out.println("未获图片流信息");
            return;
        }
        UnitTools.doTestWrite(ima, "TestPicture");
    }


    //    private String u = "https://profile-avatar.csdnimg.cn/default.jpg"; // 正图/
    private String u = "https://pic.rmb.bdstatic.com/bjh/5748977058196cd9fbdb1dd1e2aea5b68191.jpeg"; // 竖图
//    private String u = "https://pic.rmb.bdstatic.com/bjh/events/273bdad3280e8139b18d25bf4fbceaa94604.jpeg"; // 横图

    /**
     * 绘图
     *
     * @param bean 图片信息
     */
//    public void drawer(BeanPicture bean) {
//        System.out.println(bean.src);
//        BufferedImage ima = iPictureBuffer(bean.src);
//        if (null == ima) {
//            System.out.println("未获图片流信息");
//            return;
//        }
//        // 输出原图
//        UnitTools.doTestWrite(ima, "TestPicture");
//    }
    public void drawer(BeanPicture bean) {
        // 背景
        BufferedImage bf = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
        Graphics2D sd = bf.createGraphics();

        // 透明背景
        AlphaComposite aBack = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0f);
        sd.setComposite(aBack);

        // 图片
        sd.setColor(Color.WHITE);
        sd.fillRect(50, 50, 400, 400);

        // 组件不透明
        AlphaComposite aItem = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
        sd.setComposite(aItem);

        BufferedImage ima = iPictureBuffer(bean.src);

        int rw = 200;
        int rh = 300;

        // 裁剪
        ima = iPictureCut(ima, "W", rw, rh);
        System.out.println(ima.getWidth());
        System.out.println(ima.getHeight());
        // 重新生成一张对应尺寸的图片
        BufferedImage image = new BufferedImage(rw, rh, BufferedImage.TYPE_INT_ARGB);
        Graphics2D sd1 = image.createGraphics();
        sd1.drawImage(ima, 0, 0, rw, rh, null);
        sd1.dispose();

        System.out.println(image.getWidth());
        System.out.println(image.getHeight());

        // 圆角
        int LTR = 30;
        int LBR = 30;
        int RTR = 30;
        int RBR = 30;
        if (LTR > 0 || LBR > 0 || RTR > 0 || RBR > 0) {
            ima = iPictureRadius(image, LTR, LBR, RTR, RBR);
        }

        sd.drawImage(ima, 50, 50, rw, rh, null);

        try {
            ImageIO.write(bf, "png", new File(Commons.SAVE_PATH + "Picture.png"));
            sd.dispose();
        } catch (IOException e) {
            sd.dispose();
            System.out.println("图片生成出错");
        }
    }

    public BufferedImage rotateImage(BufferedImage originalImage, double degree) {
//        int width = originalImage.getHeight();
//        int height = originalImage.getWidth();
//        int width = originalImage.getWidth();
//        int height = originalImage.getHeight();
        BufferedImage rotatedImage = new BufferedImage(originalImage.getHeight(), originalImage.getWidth(), originalImage.getType());
        Graphics2D g2d = rotatedImage.createGraphics();

        // 计算图片中心点
//        int x = width / 2;
//        int y = height / 2;
        g2d.translate(originalImage.getHeight(), originalImage.getWidth());
        g2d.rotate(Math.toRadians(degree), 0, 0);
//        g2d.translate((originalImage.getWidth() - originalImage.getHeight()) / 2, (originalImage.getWidth() - originalImage.getHeight()) / 2);
//        g2d.rotate(Math.toRadians(degree), originalImage.getHeight() / 2, originalImage.getWidth() / 2);
        g2d.drawImage(originalImage, 0, 0, null);

        // 创建旋转转换
//        AffineTransform at = new AffineTransform();
//        at.rotate(Math.toRadians(degree), x, y);
////        at.translate(-x/2, -y/2);
//
//        // 执行旋转
//        AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
//        rotatedImage = op.filter(originalImage, rotatedImage);

        g2d.dispose(); // 释放图形上下文
        return rotatedImage;
    }


    /**
     * 图片裁剪
     *
     * @param buffer 流
     * @param mode   裁剪方式  T：靠上靠前 C：居中 B：靠下靠后 W：不裁剪
     */
    private BufferedImage iPictureCut(BufferedImage buffer, String mode, int rw, int rh) {
        if (null == buffer) return null;
        int tw = buffer.getWidth();
        int th = buffer.getHeight();
        // 因图 Z：正方形 H：横长方形 S：竖长方形
        String tDirection = UnitTools.toPictureDirection(tw, th);
        // 果图 Z：正方形 H：横长方形 S：竖长方形
        String rDirection = UnitTools.toPictureDirection(rw, rh);

        int X = 0;
        int Y = 0;
        float ta = (float) tw / th;
        float ra = (float) rw / rh;
        // 因正/果正
        if (tDirection.equals("Z") && rDirection.equals("Z") || ta == ra) {
            System.out.println("因正/果正/等比");
            return buffer;
        }

        // 因竖/果正
        if (tDirection.equals("S") && rDirection.equals("Z")) {
            System.out.println("因竖/果正");
            Y = mode.equals("C") ? (th - tw) / 2 : mode.equals("B") ? (th - tw) : Y;
            return buffer.getSubimage(X, Y, tw, tw);
        }

        // 因横/果正
        if (tDirection.equals("H") && rDirection.equals("Z")) {
            System.out.println("因横/果正");
            X = (mode.equals("C") ? (tw - th) / 2 : mode.equals("B") ? (tw - th) : X);
            return buffer.getSubimage(X, Y, th, th);
        }

        // 因正/果横 || 因竖/果横
        if ((tDirection.equals("Z") && rDirection.equals("H")) || (tDirection.equals("S") && rDirection.equals("H"))) {
            System.out.println("因正/果横 || 因竖/果横");
            int ith = (int) (tw / ((float) rw / rh));
            Y = mode.equals("C") ? (th - ith) / 2 : mode.equals("B") ? (th - ith) : Y;
            return buffer.getSubimage(X, Y, tw, ith);
        }

        // 因正/果竖 || 因横/果竖
        if ((tDirection.equals("Z") && rDirection.equals("S")) || (tDirection.equals("H") && rDirection.equals("S"))) {
            System.out.println("因正/果竖 || 因横/果竖");
            int itw = (int) (th / ((float) rh / rw));
            X = mode.equals("C") ? (tw - itw) / 2 : mode.equals("B") ? (tw - itw) : X;
            return buffer.getSubimage(X, Y, itw, th);
        }

        // 因竖/果竖 || 因横/果横
        if ((tDirection.equals("S") && rDirection.equals("S")) || (tDirection.equals("H") && rDirection.equals("H"))) {
            System.out.println("因竖/果竖 || 因横/果横");
            int ith = th;
            int itw = tw;
            if (ta < ra) {
                ith = (int) (tw / ((float) rw / rh));
                Y = (mode.equals("C") ? (th - ith) / 2 : mode.equals("B") ? (th - ith) : Y);
            } else {
                itw = (int) (th / ((float) rh / rw));
                X = (mode.equals("C") ? (tw - itw) / 2 : mode.equals("B") ? (tw - itw) : X);
            }
            return buffer.getSubimage(X, Y, itw, ith);
        }
        return buffer;
    }

    /**
     * 图片圆角
     *
     * @param buffer 流
     *               LTR:左上角 LBR:左下角 RTR:右上角 RBR:右下角
     */
    private BufferedImage iPictureRadius(BufferedImage buffer, int LTR, int LBR, int RTR, int RBR) {
//        rw = buffer.getWidth();
//        rh = buffer.getHeight();
        BufferedImage image = new BufferedImage(buffer.getWidth(), buffer.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D sd = image.createGraphics();

        sd.setPaint(new TexturePaint(buffer, new Rectangle(buffer.getWidth(), buffer.getHeight())));

        int max = UnitTools.onGetMaxRadius(LTR, LBR, RTR, RBR);
        int wRadius = (int) (buffer.getWidth() * ((double) max / 100)); // 宽半径
        int hRadius = (int) (buffer.getHeight() * ((double) max / 100)); // 高半径

//        double arcw = ((double) buffer.getWidth() / buffer.getWidth()) * wRadius;
//        double arch = ((double) buffer.getHeight() / buffer.getHeight()) * hRadius;
        System.out.println("====================");
        System.out.println(max);
        System.out.println(wRadius);
        System.out.println(hRadius);

//        System.out.println(arcw);
//        System.out.println(arch);
        System.out.println("---------");
        System.out.println(buffer.getWidth());
        System.out.println(buffer.getHeight());
        System.out.println("--------------");
        sd.fill(new RoundRectangle2D.Double(0, 0, buffer.getWidth(), buffer.getHeight(), wRadius * 2, hRadius * 2));
        if (LTR <= 0) { // 恢复左上角为直角
            sd.fillRect(0, 0, wRadius * 2, hRadius * 2);
        }
        if (LBR <= 0) { // 恢复左下角为直角
            sd.fillRect(0, buffer.getHeight() - hRadius * 2, wRadius * 2, hRadius * 2);
        }
        if (RTR <= 0) { // 恢复右上角为直角
            sd.fillRect(buffer.getWidth() - wRadius * 2, 0, wRadius * 2, hRadius * 2);
        }
        if (RBR <= 0) { // 恢复右下角为直角
            sd.fillRect(buffer.getWidth() - wRadius * 2, buffer.getHeight() - hRadius * 2, wRadius * 2, hRadius * 2);
        }
        return image;
    }

    // AlphaComposite.CLEAR -交集部分的颜色和透明被清除。
    // AlphaComposite.DST - 目标未修改。
    // AlphaComposite.DST_ATOP - 目标和源重叠的部分组合在源上。
    // AlphaComposite.DST_IN -显示目标和源重叠的部分。
    // AlphaComposite.DST_OUT -显示目标没有和源重叠的部分。
    // AlphaComposite.DST_OVER - 目标覆盖在源之上。
    // AlphaComposite.SRC -源复制给目标。
    // AlphaComposite.SRC_ATOP - 源和目标重叠的部分组合在目标上。
    // AlphaComposite.SRC_IN - 显示源和目标重叠的部分。
    // AlphaComposite.SRC_OUT -显示源没有和目标重叠的部分。
    // AlphaComposite.SRC_OVER - 源覆盖在目标之上

}
